package cn.xm.exception;

import cn.xm.result.AjaxResult;
import cn.xm.test3.exception.ErroCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    //拦截异常 : 这个注解就可以拦截器 GlobleException 异常
    @ExceptionHandler(BusinessException.class)
    public AjaxResult globleException(BusinessException e){
        e.printStackTrace();
        // 通过异常code获取枚举对象
        ErroCode erroCode = ErroCode.getByCode(e.getMessage());
        return AjaxResult.error(erroCode.getCode(),erroCode.getMessage());

    }


    //拦截器其他异常
    @ExceptionHandler(Exception.class)
    public AjaxResult exception(Exception e){
        e.printStackTrace();
        return AjaxResult.error("200","系统异常,正在殴打程序员...");
    }

}
